import 'package:flutter/cupertino.dart';
import 'package:flutter_axrjc/datasource/network/bean/SelectOption.dart';
import 'package:get/get.dart';

class SelectionController extends GetxController {
  RxList<SelectOption> showList = <SelectOption>[].obs;
  final selectedList = <SelectOption>[].obs;
  GlobalKey key = GlobalKey();
  var height = (Get.height / 2).obs;
  String keywords = "";
  final List<SelectOption> list;

  SelectionController(this.keywords, this.list);

  filterList() {
    showList.clear();
    if (keywords.isNotEmpty) {
      showList.addAll(list
          .where((element) => (element.label.contains(keywords) ||
              element.value.contains(keywords)))
          .toList());
    } else {
      showList.addAll(list);
    }
    refreshHeight();
  }

  @override
  void onInit() {
    super.onInit();
    filterList();
  }

  refreshHeight() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      double realityH =
          (key.currentContext!.findRenderObject() as RenderBox).size.height;
      if (realityH > Get.height / 2) {
        height.value = Get.height / 2;
      } else {
        height.value = realityH;
      }
    });
  }
}
